package edu.zsl.controller;

import edu.zsl.pojo.Student;
import edu.zsl.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class StudentController {
    private final StudentService studentService;

    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    // 加载页面
    @GetMapping("/studentpage")
    public String getStudentPage() {
        return "student";
    }
    //添加
    @GetMapping("addstudentpage")
    public String addStudentPage() {
        return "addstudent";
    }

    // 处理数据
    @GetMapping("/students")
    public String getAllStudents(Model model) {
        List<Student> students = studentService.getAllStudents();
        model.addAttribute("students", students);
        return "student";
    }

    @GetMapping("updatestudentpage/{id}")
    public String updateStudentPage(@PathVariable String id,
                                    Model model) {
        Student student = studentService.getStudentById(id);
        System.out.println(student);
        model.addAttribute("student", student);
        return "updatestudent";
    }

    @PostMapping("/updatestudent")
    public String updateStudent(@ModelAttribute Student student) {
        studentService.updateStudent(student);
        return "redirect:/students";
    }
}